package com.mail;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Sawior
 */

public class Email implements Serializable
{
   private static final long serialVersionUID = 1544680932114626710L;

   InitialContext ctx = new InitialContext();  
        Session mySession;  


   private String to;
   private String from;
   private String subject;
   private String body;

    public Email(String to, String from, String subject, String body) throws NamingException {
        this.mySession = (Session) ctx.lookup("mail/working");
        this.to = to;
        this.from = from;
        this.subject = subject;
        this.body = body;
    }

   public String getTo()
   {
      return to;
   }

   public void setTo(String to)
   {
      this.to = to;
   }

   public String getFrom()
   {
      return from;
   }

   public void setFrom(String from)
   {
      this.from = from;
   }

   public String getSubject()
   {
      return subject;
   }

   public void setSubject(String subject)
   {
      this.subject = subject;
   }

   public String getBody()
   {
      return body;
   }

   public void setBody(String body)
   {
      this.body = body;
   }

   public void send() throws Exception
   {
      Message message = new MimeMessage(mySession);
      message.setFrom(new InternetAddress(from));
      Address toAddress = new InternetAddress(to);
      message.addRecipient(Message.RecipientType.TO, toAddress);
      message.setSubject(subject);
      message.setContent(body, "UTF-8");
      Transport.send(message);
   }

   public List<String> poll() throws Exception
   {
      long start = System.currentTimeMillis();
      List<String> list = new ArrayList<String>();

      Store store = null;
      Folder inbox = null;
      try {
         // List all messages in the INBOX
         store = mySession.getStore();
         store.connect();

         inbox = store.getFolder("INBOX");

         inbox.open(Folder.READ_WRITE);

         Message[] messages = inbox.getMessages();
         for (Message m : messages) {
            System.out.println("Message subject:" + m.getSubject());
            list.add(m.getSubject());
         }
      }
      catch (Exception ex) {
         System.out.println("Unknown error occured:" + ex.getMessage());
      }
      finally {
         try {
            if (null != inbox) {
               inbox.close(true);
            }
            if (null != store) {
               store.close();
            }
         }
         catch (Exception ex) {
            System.out.println("Unable to close IMAP store connection");
         }
      }

      long end = System.currentTimeMillis();
      long diff = end - start;
      System.out.println("Found [" + list.size() + "] messages. In [" + diff + "] millis.");
      return list;
   }
}
